'use strict';

//记录用户所有状态
const socket = require('../base_net/socket');
const clientMgr = require('../majiang_server/clientMgr');    

var userLocation = {};
var userList = {};      //保存socket
var userOnline = {};    //在线人数

exports.bind = function(userId,client,type){
    exports.userAdd(type);
    userList[userId] = client;
};
exports.reconnect = function(userId,client,type){
    userList[userId] = client;
};
exports.clear = function(userId,type){
    exports.userMinus(type);
    delete userList[userId];
    delete userLocation[userId];
};
exports.setLocation = function(userId,location){
    userLocation[userId] = location;
};
exports.getLocation = function(userId){
    return userLocation[userId];
};
exports.getCity = function(userId) {
    if (userLocation[userId]) {
        return userLocation[userId].city;
    }
};
function getDistance(lat1, lng1, lat2, lng2){
    // 地球平均半径
    const EARTH_RADIUS = 6378137;
    // 把经纬度转为度（°）
    // 纬度
    let degree_lat1 = (lat1 * Math.PI) / 180.0;
    let degree_lat2 = (lat2 * Math.PI) / 180.0;
    let a = degree_lat1 - degree_lat2;
    // 经度
    let degree_lng1 = (lng1 * Math.PI) / 180.0;
    let degree_lng2 = (lng2 * Math.PI) / 180.0;
    let b = degree_lng1 - degree_lng2;
    // 距离 （单位：米）
    let s =
      2 *
      Math.asin(
        Math.sqrt(
          Math.pow(Math.sin(a / 2), 2) +
            Math.cos(degree_lat1) *
              Math.cos(degree_lat2) *
              Math.pow(Math.sin(b / 2), 2)
        )
      );
    s = s * EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000;
    return s;
}
exports.getDistance = function(userId, userId2) {
    if (userLocation[userId] == null) return;
    if (userLocation[userId2] == null) return;
    if (userLocation[userId].longitude == null || userLocation[userId].longitude < 0) return;
    if (userLocation[userId2].longitude == null || userLocation[userId2].longitude < 0) return;
    // 单位 米
    return getDistance(userLocation[userId].longitude, userLocation[userId].latitude,
                userLocation[userId2].longitude, userLocation[userId2].latitude);
};

exports.getUserOnline = function(type){
    return userOnline[type];
}
exports.getTotalUsers = function(){
    var count = 0;
    for(var i in userOnline){
        count += userOnline[i];
    }
    return count;
};
exports.userAdd = function(type){
    if(userOnline[type] >= 0){
        userOnline[type] ++;
    }
    else{
        userOnline[type] = 1;
    }
}
exports.userMinus = function(type){
    if(userOnline[type] > 0){
        userOnline[type] --;
    }
    else{
        userOnline[type] = 0;
    }
}
exports.getSocket = function(userId){
    return userList[userId];
};

exports.isOnline = function(userId){
    var data = userList[userId];
    if(data != null){
        return true;
    }
    return false;
};

/*function update() {
    for(var userId in userList) {
        var client = userList[userId];
        if(client.isActive == false){
            client.isOnline = false;
            clientMgr.broadcastInRoom(socket.resp.user_state, {userId:userId,online:false}, userId);
        }
        socket.send(client, 3, "ping");
        client.isActive = false;
    }
}
setInterval( update, 3000 );*/
